package com.eight.homepage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eight.common.pojo.CrmBanner;
import com.eight.common.utils.OssUtil;
import com.eight.homepage.dao.CrmBannerDao;
import com.eight.homepage.service.CrmBannerService;
import com.eight.homepage.vo.CrmBannerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;

/**
 * @author 向文豪
 * @program: eight-education-parent
 * @description:
 * @date 2023/5/27 19:37
 */
@Service
public class CrmBannerServiceImpl implements CrmBannerService {

    @Autowired
    private CrmBannerDao crmBannerDao;

    @Autowired
    private  OssUtil ossUtil;

    @Override
    public Page<CrmBanner> findPage(Integer currentPage, Integer pageSize, CrmBannerVo crmBannerVo) {
        Page<CrmBanner> page = new Page<>(currentPage,pageSize);
        QueryWrapper<CrmBanner> wrapper = new QueryWrapper<>();
        if(StringUtils.hasText(crmBannerVo.getTitle())){
            wrapper.like("title",crmBannerVo.getTitle());
        }
        if(StringUtils.hasText(crmBannerVo.getIsDisable())){
            wrapper.eq("is_disable",crmBannerVo.getIsDisable());
        }
        Page<CrmBanner> crmBannerPage = crmBannerDao.selectPage(page, wrapper);
        return crmBannerPage;
    }

    @Override
    public int updateStatus(String id, String isDisable) {

        CrmBanner crmBanner = new CrmBanner();
        crmBanner.setId(id);
        crmBanner.setIsDisable(isDisable);
        crmBanner.setGmtModified(LocalDateTime.now());
        int i = crmBannerDao.updateById(crmBanner);
        return i;
    }

    @Override
    public int delete(CrmBanner id) {
        int i = crmBannerDao.deleteById(id);
        return i;
    }

    @Override
    public String addBanner(MultipartFile file) {
        String path = OssUtil.uploadFile(file);
        return path;
    }

    @Override
    public int add(CrmBanner crmBanner) {
       if(StringUtils.hasText(crmBanner.getId())){
           return crmBannerDao.updateById(crmBanner);
       }

        int insert = crmBannerDao.insert(crmBanner);
        return insert;
    }
}
